package com.ktc.base.controller;

import com.ktc.base.pojo.Label;
import com.ktc.base.service.LabelService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 周罗校
 * @Description
 * @ClassName LabelController
 * @date 2020.11.18 20:05:07
 * 该版权归周罗校所有，最终解释权以官方为准
 */
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 添加
     *
     * @param label
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Label label) {
        labelService.add(label);

        return new Result(StatusCode.OK,true, "添加成功");
    }

    /**
     * 根据id删除标签
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result deleteByID(@PathVariable String id) {
        labelService.deleteById(id);

        return new Result(StatusCode.OK,true,"删除成功");
    }

    /**
     * 修改标签
     * @param id
     * @param label
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable String id, @RequestBody Label label) {
        label.setId(id);
        labelService.update(label);
        return new Result(StatusCode.OK,true,"修改成功");
    }

    /**
     * 根据标签id查询单个标签
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        Label label= labelService.findById(id);

        return new Result(StatusCode.OK,true,"查询成功", label);
    }


    /**
     * 查询全部标签
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Label> labelList = labelService.findAll();

        return new Result(StatusCode.OK,true,"查询成功", labelList);
    }

}